package com.im.partchat.ui.fragment;

import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.im.partchat.App;
import com.im.partchat.R;
import com.im.partchat.adapter.MainGirdAdapter;
import com.im.partchat.base.BaseFragment;
import com.im.partchat.bean.eventbus.NagvationToLearn;
import com.im.partchat.bean.main.BannerBean;
import com.im.partchat.bean.main.MainGirdBean;
import com.im.partchat.bean.main.MainNewData;
import com.im.partchat.mvp.persenter.GetMainDataPersenter;
import com.im.partchat.utils.GlideImageLoader;
import com.im.partchat.utils.Navigation;
import com.im.partchat.widget.NetworkStateView;
import com.im.partchat.widget.NoScrollGridView;
import com.im.partchat.widget.banner.Banner;
import com.im.partchat.widget.banner.BannerConfig;
import com.im.partchat.widget.banner.listener.OnBannerListener;

import org.greenrobot.eventbus.EventBus;
import org.reactivestreams.Publisher;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;
import io.reactivex.Flowable;
import io.reactivex.SingleObserver;
import io.reactivex.disposables.Disposable;
import io.reactivex.functions.Function;


/**
 * Created by fzl on 2017/6/26 0026.
 * VersionCode: 1
 * Desc:
 */

public class MainFragment extends BaseFragment implements MainGirdAdapter.OnItemClickListener, NetworkStateView.OnRefreshListener {
    @BindView(R.id.banner)
    Banner banner;
    @BindView(R.id.gv_main)
    NoScrollGridView gvMain;
    @BindView(R.id.tv_label)
    TextView tvLabel;
    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.iv_msg_pic)
    ImageView ivMsgPic;
    @BindView(R.id.tv_department)
    TextView tvDepartment;
    @BindView(R.id.tv_time)
    TextView tvTime;
    @BindView(R.id.tv_readNum)
    TextView tvReadNum;
    @BindView(R.id.ll_top_message)
    LinearLayout llTopMessage;

    private GetMainDataPersenter<MainNewData> getNewsPersenter;

    private List<MainGirdBean> mainGirdBeanList;
    private List<BannerBean> bannerBeanList;

    private List<MainNewData.LbsBean> lbs;
    MainNewData.EssBean essBean;

    public MainFragment() {
    }


    @Override
    protected void initView(View view, Bundle savedInstanceState) {

        initGirdView();

    }


    @Override
    protected int getLayoutId() {
        return R.layout.fragment_main;
    }

    @Override
    protected void initData() {
        getNewsPersenter = new GetMainDataPersenter(this, this);
        getNewsPersenter.getMainData();

    }

    @Override
    public void onLoadDataSuccess(Object o, String flag) {
        if (o == null) {
            showEmpty();
            networkStateView.setOnRefreshListener(this);

        } else {
            showPageSuccess();
        }
        MainNewData mainNewData = (MainNewData) o;
        lbs = mainNewData.getLbs();
        initBanner();
        List<MainNewData.EssBean> ess = mainNewData.getEss();
        if (ess == null || ess.size() == 0) {
            return;
        }
        essBean = mainNewData.getEss().get(0);
        initBottom();
    }

    private void initBottom() {

        tvTitle.setText(essBean.getTitle());
        Glide.with(this)
                .load(essBean.getImg())
                .centerCrop()
                .crossFade()
                .into(ivMsgPic);

        tvDepartment.setText(essBean.getSubInfo());
        tvReadNum.setText(essBean.getCommentNo());
        tvTime.setText(essBean.getDate());
    }

    private void initBanner() {
        if (lbs == null || lbs.size() == 0) {
            return;
        }
        Flowable.just(lbs).flatMap(new Function<List<MainNewData.LbsBean>, Publisher<MainNewData.LbsBean>>() {
            @Override
            public Publisher<MainNewData.LbsBean> apply(List<MainNewData.LbsBean> lbsBeen) throws Exception {

                return Flowable.fromIterable(lbsBeen);
            }
        }).map(new Function<MainNewData.LbsBean, BannerBean>() {
            @Override
            public BannerBean apply(MainNewData.LbsBean lbsBean) throws Exception {

                return new BannerBean(lbsBean.getUrl(), lbsBean.getTitle(), lbsBean.getImg());
            }
        }).toList()
                .subscribe(new SingleObserver<List<BannerBean>>() {
                    @Override
                    public void onSubscribe(Disposable d) {

                    }

                    @Override
                    public void onSuccess(final List<BannerBean> value) {
                        bannerBeanList = value;
                        banner.setBannerBeans(bannerBeanList);
                        banner.setImageLoader(new GlideImageLoader());
                        banner.setBannerStyle(BannerConfig.NUM_INDICATOR_TITLE);
                        banner.setDelayTime(3000);
                        banner.start();
                        banner.setOnBannerListener(new OnBannerListener() {
                            @Override
                            public void OnBannerClick(View v, int position) {
                                Navigation.navigateWebview("新闻详情", value.get(position).getUrl());
                            }
                        });
                    }

                    @Override
                    public void onError(Throwable e) {
                        showToast("解析banner数据失败");
                    }
                });
    }

    private void initGirdView() {
        mainGirdBeanList = new ArrayList<>();
        mainGirdBeanList.add(new MainGirdBean(R.drawable.icon_1, "组织机构"));
        mainGirdBeanList.add(new MainGirdBean(R.drawable.icon_2, "三会一课"));
        mainGirdBeanList.add(new MainGirdBean(R.drawable.icon_3, "政银服务"));
        mainGirdBeanList.add(new MainGirdBean(R.drawable.icon_4, "我要交党费"));
        mainGirdBeanList.add(new MainGirdBean(R.drawable.icon_5, "典型案例"));
        mainGirdBeanList.add(new MainGirdBean(R.drawable.icon_6, "网上党校"));
        mainGirdBeanList.add(new MainGirdBean(R.drawable.icon_7, "两学一做"));
        mainGirdBeanList.add(new MainGirdBean(R.drawable.icon_8, "党建微测试"));
        MainGirdAdapter mainGirdAdapter = new MainGirdAdapter(App.getApplication(), mainGirdBeanList);
        gvMain.setAdapter(mainGirdAdapter);
        mainGirdAdapter.setOnItemClickListener(this);

    }

    @OnClick(R.id.ll_top_message)
    public void onViewClicked() {
    Navigation.navigateWebview(essBean.getTitle(),essBean.getUrl());
    }

    @Override
    public void onItemClickListener(int position) {
        switch (position) {
            case 0:
                Navigation.navigateLocation();
                break;
            case 1:
                Navigation.navigateMyNote();
                break;
            case 2:
                showToast("正在建设中……");
                break;
            case 3:
                Navigation.navigatePayDues();
                break;
            case 4:
                EventBus.getDefault().post(new NagvationToLearn(4));
                break;
            case 5:
                showToast("正在建设中……");
                break;
            case 6:
                EventBus.getDefault().post(new NagvationToLearn(1));
                break;
            case 7:
                showToast("正在建设中……");
                break;
        }
    }


    @Override
    public void onRefresh() {
        if (getNewsPersenter != null) {
            getNewsPersenter.getMainData();
        }
    }
}
